var provinces = new Array("京","沪","浙","苏","粤","鲁","晋","冀",
            "豫","川","渝","辽","吉","黑","皖","鄂",
            "津","贵","云","桂","琼","青","新","藏",
            "蒙","宁","甘","陕","闽","赣","湘");

var keyNums = new Array("0","1","2","3","4","5","6","7","8","9",
            "Q","W","E","R","T","Y","U","I","O","P",
            "A","S","D","F","G","H","J","K","L",
            "OK","Z","X","C","V","B","N","M","Del");
var next=0;			
	function showProvince(){
			$("#pro").html("");
			var ss="";
			for(var i=0;i<provinces.length;i++){
				ss=ss+addKeyProvince(i)
			} 
			$("#pro").html("<ul class='clearfix ul_pro'>"+ss+"<li class='li_close' onclick='closePro();'><span>关闭</span></li><li class='li_clean' onclick='cleanPro();'><span>清空</span></li></ul>");
	} 
	function showKeybord(){
			$("#pro").html("");
			var sss="";
			for(var i=0;i<keyNums.length;i++){
				sss=sss+'<li class="ikey ikey'+i+' '+(i>9?"li_zm":"li_num")+' '+(i>28?"li_w":"")+'" ><span onclick="choosekey(this,'+i+');">'+keyNums[i]+'</span></li>'
			} 
			$("#pro").html("<ul class='clearfix ul_keybord'>"+sss+"</ul>");
	}
    function addKeyProvince(provinceIds){
        var addHtml = '<li>';
            addHtml += '<span onclick="chooseProvince(this);">'+provinces[provinceIds]+'</span>';
            addHtml += '</li>';
            return addHtml;
    }

    function chooseProvince(obj){
       $(".input_pro span").text($(obj).text());
	   $(".input_pro").addClass("hasPro");
	   $(".input_pp").find("span").text("");
       $(".ppHas").removeClass("ppHas");
	   next=0;
	   showKeybord();
	}	
	
	
	function choosekey(obj,jj){	
		if(jj==29){
			// alert("车牌："+$(".car_input").attr("data-pai"));
			layer.closeAll();
		}else if(jj==37){
			if($(".ppHas").length==0){
				$(".hasPro").find("span").text("");			
				$(".hasPro").removeClass("hasPro");	
				showProvince();
				next=0;
			}
			$(".ppHas:last").find("span").text("");			
			$(".ppHas:last").removeClass("ppHas");	
			next=next-1;
			if(next<1){
				next=0;
			}
			console.log(next);
		}else{
			if(next>6){
				return
			}
			console.log(next);
			for(var i = 0; i<$(".input_pp").length;i++){

				if(next==0 & jj<10 & $(".input_pp:eq("+next+")").hasClass("input_zim")){
					layer.open({
						content: '车牌第二位为字母',
						skin: 'msg',
						time: 1
					});
					return
				}
				$(".input_pp:eq("+next+")").find("span").text($(obj).text());
				$(".input_pp:eq("+next+")").addClass("ppHas");
				next=next+1;
				if(next>6){
					next=7;
				}
				getpai();
				return
			}
			
		}
		
		
       
	}
	function closePro(){
       layer.closeAll()
	}		
	function cleanPro(){
       $(".ul_input").find("span").text("");
       $(".hasPro").removeClass("hasPro");
       $(".ppHas").removeClass("ppHas");
	   next=0;
	}	
	function trimStr(str){return str.replace(/(^\s*)|(\s*$)/g,"");}
	function getpai(){
		var pai=trimStr($(".car_input").text());
		$(".car_input").attr("data-pai",pai);
	}
window.onload = function() {

	$(".input_pro").click(function(){
		 layer.open({
			type: 1
			,content: '<div id="pro"></div>'
			,anim: 'up'
			,shade :false 
			,style: 'position:fixed; bottom:0; left:0; width: 100%; height: auto; padding:0; border:none;'
		  });
		 showProvince()
	})
	$(".input_pp").click(function(){
		 if($(".input_pro").hasClass("hasPro")){ // 如果已选择省份
			 layer.open({
				type: 1
				,content: '<div id="pro"></div>'
				,anim: 'up'
				,shade :false 
				,style: 'position:fixed; bottom:0; left:0; width: 100%; height: auto; padding:0; border:none;'
			  });
			 showKeybord()
		 }else{
			 $(".input_pro").click()
		 }
	})
}


// 查询车牌号
function inquery() {
    var car_input=$(".car_input").attr('data-pai');
    layer.closeAll();
    if(!isEmpty(car_input))
	{
        var carNum=car_input.replace(/[\r\n]/g,"").replace(/[ ]/g,"");
        var address=location.href;
        var appid=GetQueryString("appid");
        var cappid=getcookie("appid");
        var wxopenid=getcookie("wxopenid")
        $(".flipper")	.addClass("rotate")
        if(appid != null ||  cappid != null){
            var data = {
                "openId":wxopenid,
                "carNum":carNum
            };
            $.ajax({
                url:ApiUrl+"/api/feemount/search",
                headers:{"appId":appid,"authorization":auth},
                type: "post",
                data:JSON.stringify(data) ,
                contentType: "application/json",
                success: function (result) {
                    if (result.code == 200) {
                        $(".flipper")	.addClass("rotate")
                    // $(".loading").css("display","none")
                    // $(".container").css("display","block")
                    // var datas=result.data;
                    // console.log(datas)
                    // $(".payNum span").html(datas.packingFee);
                    // $(".carNum").html(carNum);
                    // $(".parkTime").html(datas.entryTime);
                    // $(".totalTime").html(datas.packingTime);
                    // $(".totalMoney").html(datas.packingFee);
               		 }
                },
				fail:function () {
                    layer.open({
                        content: '请求失败，请稍后再试',
                        skin: 'msg',
                        time: 1
                    });
                }

            });
        }
        else {
            layer.open({
                content: 'appid不能为空',
                skin: 'msg',
                time: 1
            });
        }
	}
	else {
        layer.open({
            content: '请输入车牌号',
            skin: 'msg',
            time: 1
        });
	}
}




// ------------------微信支付begin------------------------
function onBridgeReady(datas){
    var request = {
        appId:datas.appId,     //公众号名称，由商户传入
        timeStamp:datas.timeStamp,         //时间戳，自1970年以来的秒数
        nonceStr:datas.nonceStr, //随机串
        package:datas.package,
        signType:datas.signType,         //微信签名方式：
        paySign:datas.paySign //微信签名
    };
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', request,
        //调起微信支付成功
        function(res) {
            WeixinJSBridge.log("支付"+res.err_msg);
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                //alert("微信支付成功!");
                // window.location.href = "http://你的域名/weChatpay/resultServlet?openId=" + openId + "&num=" + num;
                window.location.href="success.html"
            }
            else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                //alert("用户取消支付!");
            }
            else {
                alert("支付失败"+JSON.stringify(res))
                //支付失败或取消，恢复到原来的订单量

            }
        }
    );
}

// 微信浏览器内部调用微信支付
function wxPay() {
    var carNum=$(".carNum").html();
    var cappid=getcookie("appid");
    var wxopenid=getcookie("wxopenid")
    var data={
        "payChannel":"WeChatPay",
        "carNumber":carNum,
        "openId":wxopenid,
        "tradeType":"JSAPI"
    }
    var appid=GetQueryString("appid")

    if(!isEmpty(appid)||!isEmpty(cappid)){
        $.ajax({
            url:ApiUrl+"/api/js/parameters ",
            headers:{"appid":appid,"authorization":auth},
            data:JSON.stringify(data) ,
            type: "post",
            contentType: "application/json",
            success: function (result) {
                // 获取微信支付参数
                var datas=result.data;
                if (typeof WeixinJSBridge == "undefined"){
                    if( document.addEventListener ){
                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady(datas), false);
                    }else if (document.attachEvent){
                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady(datas));
                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(datas));
                    }
                }else{
                    onBridgeReady(datas);
                }

            }
        });
    }
}
//微信浏览器外调用微信支付
function wxPayE() {
    var carNum=$(".carNum").html();
    var cappid=getcookie("appid");
    var wxopenid=getcookie("wxopenid")
    var data={
        "pay_channel":"WeChatPay",
        "carNumber":carNum,
        "openId":"",
        "tradeType":"MWEB"
    }
    var appid=GetQueryString("appid")
    if(!isEmpty(appid)||!isEmpty(cappid)){
        $.ajax({
            url:ApiUrl+"/api/js/parameters ",
            headers:{"appid":appid,"authorization":auth,},
            data:JSON.stringify(data) ,
            type: "post",
            contentType: "application/json",
            success: function (result) {
                // 获取微信支付参数
                var datas=result.data;
                window.location.href=datas.mweb_url; // 后更改页面地址
            }
        });
    }
}
// ------------------微信支付end------------------------



// ------------------支付宝支付begin------------------------
function aliPay() {

    var carNum=$(".carNum").html();
    var cappid=getcookie("appid");
    var wxopenid=getcookie("aliuserid")
    var data={
        "payChannel":"AliPay",
        "carNumber":carNum
    }
    var appid=GetQueryString("appid")
    if(!isEmpty(appid)||!isEmpty(cappid)){
        $.ajax({
            url:ApiUrl+"/api/js/parameters ",
            headers:{"appid":appid,"authorization":auth},
            data:JSON.stringify(data) ,
            type: "post",
            contentType: "application/json",
            success: function (result) {
                console.log(result)
                var form = result.data
                $("#alipayForm").append(form);
                $("#alipayForm script").remove();
                var queryParam = '';
                Array.prototype.slice.call(document.querySelectorAll("input[type=hidden]")).forEach(function (ele) {
                    queryParam += ele.name + "=" + encodeURIComponent(ele.value) + '&';
                });
                var url = document.getElementsByName("punchout_form")[0].action+'&'+queryParam;
                console.log(url)
                // _AP.pay(url);
            },
            error: function(xhr, type, errorThrown) {

            }
        });
    }
}

// ------------------支付宝支付end------------------------


function BBCPay() {
    var carNum=$(".carNum").html();
    var cappid=getcookie("appid");
    var wxopenid=getcookie("wxopenid")
    var data={
        "pay_channel":"BBCPay",
        "carNumber":carNum,
        "openId":"",
    }
    var appid=GetQueryString("appid")
    if(!isEmpty(appid)||!isEmpty(cappid)){
        $.ajax({
            url:ApiUrl+"/api/js/parameters ",
            headers:{"appid":appid,"authorization":auth,},
            data:JSON.stringify(data) ,
            type: "post",
            contentType: "application/json",
            success: function (result) {

            }
        });
    }
}